//
//  BJPlayingMethodViewController.m
//  BowJoy
//
//  Created by  on 2024/5/22.
//

#import "BJPlayingMethodViewController.h"
#import <SVGAPlayer/SVGAPlayer.h>
#import "SVGAParser.h"
#import "BJRewardPopView.h"
#import "BJRewardAginPopView.h"
#import "BJPictureFrameViewController.h"

@interface BJPlayingMethodViewController ()<SVGAPlayerDelegate>

@property (weak, nonatomic) IBOutlet UIView *bgGoundView;

@property (nonatomic)UIView *scrollViewBgView;

@property (nonatomic)UIScrollView *scrollView;

@property (nonatomic, strong) SVGAPlayer *player;
@property(nonatomic) NSInteger checkNum;
@property (nonatomic, strong) UILabel *checkNumL;

@property (nonatomic) BOOL isClick;

@property(nonatomic) NSMutableArray *dataList;
@property(nonatomic) UIView *selectIcon1;
@property(nonatomic) UIView *selectIcon2;
@property(nonatomic) UIView *selectIcon3;
@property(nonatomic) UIView *selectIcon4;
@property(nonatomic)NSInteger selectIndex;
@end


@implementation BJPlayingMethodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.checkNum = [[[NSUserDefaults standardUserDefaults] objectForKey:KCheckNumKey] integerValue];
    self.selectIndex = [[[NSUserDefaults standardUserDefaults] objectForKey:KIsAvatarFrameKey] integerValue];

    [self initUI];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //埋点
    NSMutableArray *dataList = [NSMutableArray array];
    NSDictionary *dict = @{
        @"page":@"play",
        @"duration":@(0),
        @"event":@"pv",
        @"tm":@([[BJNetManager shareNetManager] gs_getCurrentTimeToMilliSecond]),
    };
    [dataList addObject:dict];
    [[BJNetManager shareNetManager]postLogWithParam:dataList];
        
    
    self.checkNum = [[[NSUserDefaults standardUserDefaults] objectForKey:KCheckNumKey] integerValue];
    self.checkNumL.text = [NSString stringWithFormat:@"x%ld",self.checkNum];

}



- (void)initUI{
    
    self.player = [[SVGAPlayer alloc] init];
   [self.view addSubview:self.player];
    self.player.makeCons(^{
        make.edge.constants(0);
    });

    
    self.player.delegate=self;
//    /动画循环次数 默认是0(无限次)
    self.player.loops=1;
     //动画停止后清除画布。 默认是true
    self.player.clearsAfterStop = false;
     //默认为Forward。可能是Forward，Backward。Forward 表示动画将在完成后在最后一帧暂停。Backward 表示动画将在完成后在第一帧暂停。
    self.player.fillMode=@"Backward";
     //设置第一帧结束的时候触发
    //    [self svgaPlayerDidAnimatedToFrame:1];
     //当图片百分比出现的时候触发
    //    [self svgaPlayerDidAnimatedToPercentage:1.0];
    [self.player stopAnimation];
     //Angel 是本地格式为svga 的文件
    SVGAParser *parser = [[SVGAParser alloc]init];
    [parser parseWithNamed:@"archeryDynamiCeffect" inBundle:[NSBundle mainBundle] completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
    if(videoItem !=nil) {
          self.player.videoItem= videoItem;
//        [self.player startAnimation];
        }
    }failureBlock:^(NSError*_Nonnullerror) {

    }];

    self.scrollViewBgView = [[UIView alloc]init];
    [self.view addSubview:self.scrollViewBgView];
    self.scrollViewBgView.makeCons(^{
        make.left.constants(0);
        make.right.constants(0);
        make.height.constants(165);
        make.top.constants(50);
    });
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    self.scrollView.contentSize = CGSizeMake(150*4, 165);
    self.scrollView.directionalLockEnabled = YES;
    [[NSUserDefaults standardUserDefaults] setInteger:self.selectIndex forKey:KIsAvatarFrameKey];

    for (int i = 0; i<4; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_play_PictureFrame%d",i]];
        imageView.tag = 1+i;
        [self.scrollView addSubview:imageView];
        imageView.makeCons(^{
            make.left.constants(150*i);
            make.width.constants(140);
            make.height.constants(165);
            make.top.constants(0);
        });
        kWeakSelf(weakSelf)
        imageView.onClick(^{
            if (imageView.tag == self.selectIndex) {
                BJPictureFrameViewController *pictureFrameView = [[BJPictureFrameViewController alloc]init];
                [weakSelf.navigationController pushViewController:pictureFrameView animated:YES];
            }

        });

        
        UIView *grayView = [[UIView alloc] init];
        grayView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.25];
        grayView.layer.masksToBounds = YES;
        grayView.layer.cornerRadius = 12;
        [imageView addSubview:grayView];
        if (self.selectIndex) {
            if (i==self.selectIndex-1) {
                grayView.hidden = YES;
            }else{
                grayView.hidden = NO;
            }
        }

        grayView.makeCons(^{
            make.center.constants(0);
            make.width.constants(130);
            make.height.constants(150);
        });
        
        
        if (i==0) {
            self.selectIcon1 = grayView;

        }else if (i==1){
            self.selectIcon2 = grayView;

        }else if (i==2){
            self.selectIcon3 = grayView;

        }else if (i==3){
            self.selectIcon4 = grayView;
        }
        
    }
    [self.scrollViewBgView addSubview:self.scrollView];
    
    
    UIButton *button = [[UIButton alloc]init];
    [button addTarget:self action:@selector(startAnimation) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    button.makeCons(^{
        make.centerX.view(self.view);
        make.width.constants(kScreenW);
        make.height.constants(200);
        make.bottom.constants(-80);
    });
    
    UIView *arrowView = [[UIView alloc]init];
    arrowView.backgroundColor = [UIColor whiteColor];
    arrowView.layer.masksToBounds = YES;
    arrowView.layer.cornerRadius = 14;
    [self.view addSubview:arrowView];
    arrowView.makeCons(^{
        make.right.view(self.view).constants(-20);
        make.height.constants(28);
        make.bottom.constants(-300);
    });
    
    UIImageView *arrowImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_arrow_right"]];
    [arrowView addSubview:arrowImage];
    arrowImage.makeCons(^{
        make.left.constants(10);
        make.width.constants(35);
        make.height.constants(28);
        make.centerY.constants(0);
    });


    [arrowView addSubview:self.checkNumL];
    self.checkNumL.makeCons(^{
        make.left.view(arrowImage).right.constants(5);
        make.right.constants(-10);
        make.centerY.constants(0);
    });


}

-(UILabel *)checkNumL{
    if(!_checkNumL){
        _checkNumL = [[UILabel alloc]init];
        _checkNumL.font = [UIFont systemFontOfSize:20];
        _checkNumL.text = [NSString stringWithFormat:@"x%ld",self.checkNum];
        _checkNumL.textColor = [UIColor blackColor];
    }
    return _checkNumL;
}


-(void)startAnimation{
    if (!self.isClick){
        //防多次点击处理
        self.isClick = YES;
        [self performSelector:@selector(avoidRepeatClick) withObject:nil afterDelay:2];
        NSInteger  checkNum = [[[NSUserDefaults standardUserDefaults] objectForKey:KCheckNumKey] integerValue];
            if (checkNum>0) {
                [self.player startAnimation];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.checkNum --;
                    self.checkNumL.text = [NSString stringWithFormat:@"x%ld",self.checkNum];
                    [[NSUserDefaults standardUserDefaults] setInteger:self.checkNum forKey:KCheckNumKey];
                    BJRewardPopView *rewardPopView = [[BJRewardPopView alloc]initWithTitle:@"Congratulations" andContent:@"Congratulations on unlocking the paid skin, go buy it now"];
                    [rewardPopView showView];
                    int randomNumber = arc4random_uniform(4) + 1;
                    self.selectIndex = randomNumber;

                    if (randomNumber == 1) {
                        self.selectIcon1.hidden = YES;
                    }else if (randomNumber == 2){
                        self.selectIcon2.hidden = YES;
                    }else if (randomNumber == 3){
                        self.selectIcon3.hidden = YES;
                    }else if (randomNumber == 4){
                        self.selectIcon4.hidden = YES;
                    }
                    

                    [[NSUserDefaults standardUserDefaults] setInteger:self.selectIndex forKey:KIsAvatarFrameKey];
                    
                });
            }else{
                BJRewardAginPopView *rewardAginPopView = [[BJRewardAginPopView alloc]initWithTitle:@"Mistake" andContent:@"Check in every day to get arrows"];
                [rewardAginPopView showView];
            }
    }
}
-(void)avoidRepeatClick{
    self.isClick = NO;
}


@end
